package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.dto.OrdersDto;
import com.example.reggie.entity.OrderDetail;
import com.example.reggie.entity.Orders;
import com.example.reggie.entity.ShoppingCart;
import com.example.reggie.service.OrderDetailService;
import com.example.reggie.service.OrdersService;
import com.example.reggie.service.ShoppingCartService;
import com.example.reggie.service.impl.ShoppingCartServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 老肥！！！
 * @Date: 2022/11/19/20:21
 * @Description: 不想学代码了！！！
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;

//     用户下单
    @PostMapping("/submit")
    public R<String>  submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return  R.success("用户下单成功");
    }
    @GetMapping("/userPage")
    public R<Page> Userpage(int page, int pageSize){
//        获取用户id
        Long userId = BaseContext.getCurrentID();
//      分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);
//        条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//        查询当前用户id订单数据
        queryWrapper.eq(userId != null,Orders::getUserId,userId);
//        按时间降序排序
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,queryWrapper);
//        获取orderID，根据这个id去orderDetail表中查数据
        List<OrdersDto> list =  pageInfo.getRecords().stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> details = orderDetailService.list(wrapper);
            BeanUtils.copyProperties(item,ordersDto);
//            设置属性
            ordersDto.setOrderDetails(details);
            return ordersDto;
        }).collect(Collectors.toList());
        BeanUtils.copyProperties(pageInfo, ordersDtoPage, "records");
        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
//        获取用户id
        Long userId = BaseContext.getCurrentID();
//      分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);
//        条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//        查询当前用户id订单数据
        queryWrapper.eq(userId != null,Orders::getUserId,userId);
//        按时间降序排序
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,queryWrapper);
//        获取orderID，根据这个id去orderDetail表中查数据
        List<OrdersDto> list =  pageInfo.getRecords().stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderDetail::getOrderId,id);
            List<OrderDetail> details = orderDetailService.list(wrapper);
            BeanUtils.copyProperties(item,ordersDto);
//            设置属性
            ordersDto.setOrderDetails(details);
            return ordersDto;
        }).collect(Collectors.toList());
        BeanUtils.copyProperties(pageInfo, ordersDtoPage, "records");
        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }
    @PutMapping
    public  R<String> update(@RequestBody Map<String,String> map){
        int status = Integer.parseInt(map.get("status"));
        Long id = Long.valueOf(map.get("id"));
        LambdaUpdateWrapper<Orders> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(Orders::getId,id);
        queryWrapper.set(Orders::getStatus,status);
        ordersService.update(queryWrapper);
        return  R.success("更新状态成功");
    }
    @PostMapping("/again")
    public R<String> again(@RequestBody Map<String,String> map){
//        获取订单id
        Long currentID = Long.valueOf(map.get("id"));
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
//        查询订单口味细节数据
        queryWrapper.eq(OrderDetail::getOrderId,currentID);
        List<OrderDetail> list = orderDetailService.list(queryWrapper);
//        获取用户id
        Long id = BaseContext.getCurrentID();
        List<ShoppingCart> shoppingCarts = list.stream().map((item)->{
            ShoppingCart shoppingCart = new ShoppingCart();
//            赋值对应属性
            BeanUtils.copyProperties(item,shoppingCart);
            shoppingCart.setUserId(id);
            shoppingCart.setCreateTime(LocalDateTime.now());
            return  shoppingCart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(shoppingCarts);
        return R.success("再来一单！");
    }
}
